package com.looker.job.zhang.alex.spider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

public class Spider51job {

	private static Connection connection = null;
	private static Channel channel = null;

	public static void main(String[] args) {
		Date startTime = new Date();
		try {
			init();
		} catch (IOException | TimeoutException e1) {
			e1.printStackTrace();
		}
		Thread bj = new Thread() {
			@Override
			public void run() {
				// 北京
				Long pageNo = 1L;
				while (true) {
					String url = "https://search.51job.com/list/010000,000000,0000,00,0,99,%2B,2," + pageNo
							+ ".html?lang=c&stype=1&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=5&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";
					try {
						Document doc = Jsoup.connect(url).get();
						Elements elements = doc.select(".t1");
						if (elements.size() < 2) {
							break;
						}
						for (int i = 1; i < elements.size(); i++) {
							String href = elements.get(i).select("a").first().attr("href");
							System.out.println(href);
							try {
								sendUrl(href);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
					++pageNo;
				}
			}
		};
		bj.start();
		Thread sh = new Thread() {
			@Override
			public void run() {
				// 上海
				Long pageNo = 1L;
				while (true) {
					String url = "https://search.51job.com/list/020000,000000,0000,00,9,99,%2520,2," + pageNo
							+ ".html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";
					try {
						Document doc = Jsoup.connect(url).get();
						Elements elements = doc.select(".t1");
						if (elements.size() < 2) {
							break;
						}
						for (int i = 1; i < elements.size(); i++) {
							String href = elements.get(i).select("a").first().attr("href");
							System.out.println(href);
							try {
								sendUrl(href);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
					++pageNo;
				}
			}
		};
		sh.start();
		Thread gz = new Thread() {
			public void run() {
				// 广州
				Long pageNo = 1L;
				while (true) {
					String url = "https://search.51job.com/list/030200,000000,0000,00,9,99,%2520,2," + pageNo
							+ ".html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";
					try {
						Document doc = Jsoup.connect(url).get();
						Elements elements = doc.select(".t1");
						if (elements.size() < 2) {
							break;
						}
						for (int i = 1; i < elements.size(); i++) {
							String href = elements.get(i).select("a").first().attr("href");
							System.out.println(href);
							try {
								sendUrl(href);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
					++pageNo;
				}
			}
		};
		gz.start();
		Thread sz = new Thread() {
			public void run() {
				// 深圳
				Long pageNo = 1L;
				while (true) {
					String url = "https://search.51job.com/list/040000,000000,0000,00,9,99,%2520,2," + pageNo
							+ ".html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=";
					try {
						Document doc = Jsoup.connect(url).get();
						Elements elements = doc.select(".t1");
						if (elements.size() < 2) {
							break;
						}
						for (int i = 1; i < elements.size(); i++) {
							String href = elements.get(i).select("a").first().attr("href");
							System.out.println(href);
							try {
								sendUrl(href);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
					++pageNo;
				}

			}
		};
		sz.start();
		try {
			bj.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			sh.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			gz.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			sz.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			destroy();
		} catch (IOException | TimeoutException e) {
			e.printStackTrace();
		}
		Date endTime = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sdf.format(startTime) + "-" + sdf.format(endTime));
	}

	private static void init() throws IOException, TimeoutException {
		ConnectionFactory factory = new ConnectionFactory();
		// "guest"/"guest" by default, limited to localhost connections
		factory.setUsername("admin");
		factory.setPassword("Renjie999");
		factory.setVirtualHost("/");
		factory.setHost("localhost");
		factory.setPort(5672);

		connection = factory.newConnection();
		// 从连接中创建通道
		channel = connection.createChannel();

	}

	private static void destroy() throws IOException, TimeoutException {
		// 关闭通道和连接
		channel.close();
		connection.close();
	}

	private static void sendUrl(String url) throws IOException, TimeoutException {

		/*
		 * 声明（创建）队列 参数1：队列名称 参数2：为true时server重启队列不会消失
		 * 参数3：队列是否是独占的，如果为true只能被一个connection使用，其他连接建立时会抛出异常
		 * 参数4：队列不再使用时是否自动删除（没有连接，并且没有未处理的消息) 参数5：建立队列时的其他参数
		 */
		channel.queueDeclare("loadContent51jobMq", true, false, false, null);

		// 消息内容
		String message = url;
		/*
		 * 向server发布一条消息 参数1：exchange名字，若为空则使用默认的exchange 参数2：routing key 参数3：其他的属性
		 * 参数4：消息体 RabbitMQ默认有一个exchange，叫default exchange，它用一个空字符串表示，它是direct
		 * exchange类型， 任何发往这个exchange的消息都会被路由到routing key的名字对应的队列上，如果没有对应的队列，则消息会被丢弃
		 */
		channel.basicPublish("", "loadContent51jobMq", MessageProperties.PERSISTENT_BASIC, message.getBytes());
		System.out.println(" [生产者] Sent '" + message + "'");

	}
}
